//index.js
//获取应用实例
const activityService = require('../../services/activityService');
var app = getApp()
Page({
  data: {
    motto: '小店之家',
    userInfo: {},
    product: {},
    images: [],
    se: '',
    fangweiInfo: '',
    num_time: ''
  },
  imageLoad: function(e) {
     var $width=e.detail.width,    //获取图片真实宽度
         $height=e.detail.height;
      var image=this.data.images;      //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
      var percent = e.target.dataset.percent;
      if(e.target.dataset.type == 'swiper') {
        var screenWidth_rpx = wx.getSystemInfoSync().screenWidth;
        var ratio=$width/$height;    //图片的真实宽高比例
        var viewWidth=screenWidth_rpx,           //设置图片显示宽度，左右留有16rpx边距
            viewHeight=screenWidth_rpx/ratio;    //计算的高度值
        if(percent) {
          viewWidth = viewWidth * percent;
          viewHeight = viewHeight * percent;
        }
        image[e.target.dataset.key]={
         width:viewWidth,
         height:viewHeight
        }
        //轮播图片最小高度
        if(this.data.swiperHeight == 0 || this.data.swiperHeight > viewHeight) {
          this.setData({
            swiperHeight: viewHeight
          });
        }
      } else {
        image[e.target.dataset.key]={
         width:$width,
         height:$height
        }
      }
      
      this.setData({
           images:image
      })
  },
  _getProduct: function(id) {
      var that = this; 
      // 获取产品信息
      activityService.getProductInfo(id).then((data=>{
        that.setData({
          product: data.data
        });
      }),(error=>{
        wx.showToast({title:'获取产品信息失败'});
      }));
  },
  onLoad: function (options) {
    var that = this; 
    // if(options.num_time == 'undefined') {
    //   options.num_time = '';
    // }
    this.setData({
      se: options.se,
      fangweiInfo: options.fangweiInfo,
      num_time: options.num_time
    });
    this._getProduct(options.pid);
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  }
})
